<template>
  <div>
    <h1> {{id? '编辑': '新建'}}英雄</h1>
    <el-form label-width='100px' @submit.native.prevent='save'>
      <el-tabs type="border-card">
        <el-tab-pane label='基本信息'>
          <el-form-item label='英雄名称'>
            <el-input v-model="model.name"></el-input>
          </el-form-item>
          <el-form-item label='英雄称号'>
            <el-input type='text' v-model="model.title"></el-input>
          </el-form-item>
          <el-form-item label='英雄头像'>
            <el-upload class="avatar-uploader" :action="uploadUrl" :headers="getAuthorization()" :show-file-list="false"
              :on-success="res=>$set(this.model, 'avatar', res.url)">
              <img v-if="model.avatar" :src="model.avatar" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label='banner'>
            <el-upload class="avatar-uploader banner" :action="uploadUrl" :headers="getAuthorization()"
              :show-file-list="false" :on-success="res=>$set(this.model, 'banner', res.url)">
              <img v-if="model.banner" :src="model.banner" class="avatar" style="width:auto">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label='英雄定位'>
            <el-select v-model="model.categories" multiple>
              <el-option v-for="item in categories" :key='item._id' :label='item.name' :value='item._id'></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label='顺风出装'>
            <el-select v-model="model.items1" multiple>
              <el-option v-for="item in items" :key='item._id' :label='item.name' :value='item._id'></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label='逆风出装'>
            <el-select v-model="model.items2" multiple>
              <el-option v-for="item in items" :key='item._id' :label='item.name' :value='item._id'></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label='难度'>
            <el-rate v-model="model.scores.difficult" :max=9 show-score style='marginTop: 0.6rem'></el-rate>
          </el-form-item>
          <el-form-item label='技能'>
            <el-rate v-model="model.scores.skill" :max=9 show-score style='marginTop: 0.6rem'></el-rate>
          </el-form-item>
          <el-form-item label='攻击'>
            <el-rate v-model="model.scores.attack" :max=9 show-score style='marginTop: 0.6rem'></el-rate>
          </el-form-item>
          <el-form-item label='生存'>
            <el-rate v-model="model.scores.existence" :max=9 show-score style='marginTop: 0.6rem'></el-rate>
          </el-form-item>
          <el-form-item label='使用技巧'>
            <el-input type='textarea' v-model='model.useTips'></el-input>
          </el-form-item>
          <el-form-item label='对抗技巧'>
            <el-input type='textarea' v-model='model.battleTips'></el-input>
          </el-form-item>
          <el-form-item label='团战技巧'>
            <el-input type='textarea' v-model='model.teamTips'></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label='技能' name='skill'>
          <el-button type='text' @click="model.skills.push({})" size='small'><i class='el-icon-plus'>添加技能</i>
          </el-button>
          <el-row type='flex' style="flex-wrap:wrap">
            <el-col :md=12 v-for="(item, i) in model.skills" :key='i'>
              <el-form-item label='名称'>
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label='技能图标'>
                <el-upload class="avatar-uploader" :action="uploadUrl" :headers="getAuthorization()"
                  :show-file-list="false" :on-success="res=>$set(item, 'icon', res.url)">
                  <img v-if="item.icon" :src="item.icon" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label='冷却'>
                <el-input v-model="item.delay"></el-input>
              </el-form-item>
              <el-form-item label='消耗'>
                <el-input v-model="item.useup"></el-input>
              </el-form-item>
              <el-form-item label='小提示'>
                <el-input type='textarea' v-model="item.tips"></el-input>
              </el-form-item>
              <el-form-item label='描述'>
                <el-input type='textarea' v-model="item.description"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type='danger' @click="model.skills.splice(i, 1)" size='small'>删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>

        <el-tab-pane label='最佳搭档'>
          <el-button type='text' @click="model.partner.push({})" size='small'><i class='el-icon-plus'>添加英雄</i>
          </el-button>
          <el-row type='flex' style="flex-wrap:wrap">
            <el-col :md=12 v-for="(item, i) in model.partner" :key='i'>
              <el-form-item label='名称'>
                <el-select filterable v-model="item.heroes">
                  <el-option v-for="hero in heroes" :key='hero._id' :label='hero.name' :value='hero._id'></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label='描述'>
                <el-input type='textarea' v-model="item.description"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type='danger' @click="model.partner.splice(i, 1)" size='small'>删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item>
        <!-- native-type: 表示其原生类型是submit -->
        <el-button type='primary' size='small' native-type='submit' style='marginTop:1rem'>保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  props: {
    id: {},
  },
  data() {
    return {
      model: {
        avatar: "",
        banner: "",
        useTips: "",
        battleTips: "",
        teamTips: "",
        scores: {
          difficult: 0,
          skill: 0,
          attack: 0,
          existence: 0,
        },
        skills: [],
        partner: [],
      },
      categories: [],
      items: [],
      heroes: [],
    };
  },
  methods: {
    async save() {
      let res;
      if (this.id) {
        // id存在则证明此时是编辑保存操作 => 更新数据
        res = await this.$http.put(`rest/heroes/${this.id}`, this.model);
      } else {
        // 否则是新建保存操作 => 新增
        res = await this.$http.post("rest/heroes", this.model);
      }
      if (res.data) {
        this.$message({
          type: "success",
          message: "保存成功",
        });
        this.$router.push("/heroes/list");
      }
    },
    // 根据id查询分类信息
    async fetchById() {
      const res = await this.$http.get(`rest/heroes/${this.id}`);
      // this.model = res.data;
      this.model = Object.assign({}, this.model, res.data);
    },
    async fetchCategories() {
      const res = await this.$http.get("rest/categories");
      this.categories = res.data;
    },
    async fetchHeroes() {
      const res = await this.$http.get("rest/heroes");
      this.heroes = res.data;
    },
    async fetchItems() {
      const res = await this.$http.get("rest/items");
      this.items = res.data;
    },
  },
  created() {
    if (this.id) {
      this.fetchById();
    }
    this.fetchCategories();
    this.fetchItems();
    this.fetchHeroes();
  },
};
// self.xhr.send(payload) 注释该行不会报sockio.js错误
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 5rem;
  height: 5rem;
  line-height: 5rem !important;
  text-align: center;
}
.avatar {
  width: 5rem;
  height: 5rem;
  display: block;
}
</style>